package com.alen.sun.service.Impl;

import com.alen.sun.bean.Activity;
import com.alen.sun.bean.Admin;
import com.alen.sun.bean.PageBean;
import com.alen.sun.dao.ActivityMapper;
import com.alen.sun.service.ActivityService;
import com.alen.sun.util.PhoneCode;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Service
public class ActivityServiceImpl implements ActivityService {
    @Autowired
    private ActivityMapper activityMapper;

    /**
     * 添加活动
     * @param activity
     * @return
     */
    @Override
    public Boolean addActivity(Activity activity) {
        //先判断必须参数否是不为空
        if(activity.getActivityName()!=null && !activity.getActivityName().isEmpty()
            && activity.getActivityDescription()!=null && !activity.getActivityDescription().isEmpty()
            && activity.getActivityTime()!=null && !activity.getActivityTime().equals(new Date(0))
            && activity.getCategoryId()!=null && !activity.getCategoryId().isEmpty()
            && activity.getActivityImage()!=null && !activity.getActivityImage().isEmpty()){
            //生成活动id
            PhoneCode phoneCode = new PhoneCode();
            while (true){
                String activityId = phoneCode.generateCode();
                //查询id是否重复
                Integer count = activityMapper.selectByActivityId(activityId);
                if(count == null || count == 0){
                    //没有记录
                    activity.setActivityId(activityId);
                    //跳出循环
                    break;
                }
            }
            //添加活动记录
            Boolean flag = activityMapper.addActivity(activity);
            return flag;
        }
        return false;
    }

    /**
     * 修改活动信息
     * @param activity
     * @return
     */
    @Override
    public Boolean updateActivity(Activity activity) {
        //先判断必须参数否是不为空
        if(activity.getActivityName()!=null && !activity.getActivityName().isEmpty()
                && activity.getActivityDescription()!=null && !activity.getActivityDescription().isEmpty()
                && activity.getActivityTime()!=null && !activity.getActivityTime().equals(new Date(0))
                && activity.getCategoryId()!=null && !activity.getCategoryId().isEmpty()
                && activity.getActivityImage()!=null && !activity.getActivityImage().isEmpty()
                && activity.getActivityId()!=null && !activity.getActivityId().isEmpty()){
            Boolean flag = activityMapper.updateActivity(activity);
            return flag;
        }
        return false;
    }

    /**
     * 删除活动
     * @param activity
     * @return
     */
    @Override
    public Boolean deleteActivity(Activity activity) {
        Boolean flag = activityMapper.deleteActivity(activity);
        return flag;
    }

    /**
     * 分页查询
     * @param page
     * @param pageSize
     * @return
     */
    @Override
    public PageBean page(Integer page, Integer pageSize) {
        //1. 设置分页参数
        //计算参数start
        Integer start = (page-1)*pageSize;
        List<Activity> activityList = activityMapper.page(start, pageSize);
        //2.查询总数
        Long count = activityMapper.count();
        PageBean pageBean = new PageBean(count,activityList);
        return pageBean;
    }

    /**
     * 批量删除
     * @param ids
     */
    @Override
    public Boolean deleteByIds(List<String> ids) {
        Boolean flag = activityMapper.deleteByIds(ids);
        return flag;
    }
}
